package com.longcai.cm.moudules.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longcai.cm.base.service.BaseServiceImpl;
import com.longcai.cm.moudules.system.bean.MenuTreeBean;
import com.longcai.cm.moudules.system.dao.SysMenusMapper;
import com.longcai.cm.moudules.system.dao.SysRoleMenusMapper;
import com.longcai.cm.moudules.system.domain.SysMenus;
import com.longcai.cm.moudules.system.domain.SysRoleMenus;
import com.longcai.cm.moudules.system.service.SysMenusService;
import com.longcai.cm.moudules.system.service.SysUsersService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service(value = "menuService")
@Transactional(rollbackFor = Exception.class)
public class SysMenusServiceImpl extends BaseServiceImpl<SysMenus> implements SysMenusService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private SysMenusMapper menusMapper;

    @Resource
    private SysRoleMenusMapper sysRoleMenusMapper;

    @Resource
    private SysUsersService sysUsersService;

    @Override
    public List<SysMenus> getAllOrderedMenuList() {

        QueryWrapper<SysMenus> eWrapper = new QueryWrapper<SysMenus>();
        eWrapper.orderByAsc("order_no");
        List<SysMenus> allMenuList = menusMapper.selectList(eWrapper);

        return allMenuList;
    }

    @Override
    public List<SysMenus> getAllPagedMenuList() {
        QueryWrapper<SysMenus> eWrapper3 = new QueryWrapper<SysMenus>();
        eWrapper3.ne("type", 2);
        eWrapper3.orderByAsc("order_no");
        List<SysMenus> allMenuList = menusMapper.selectList(eWrapper3);
        return allMenuList;
    }

    @Override
    public List<MenuTreeBean> getMenuMeanListByUserId(String userId) {

        List<MenuTreeBean> rootMenu = new ArrayList<>();


        List<SysRoleMenus> roleMenuList = sysUsersService.getRoleMenuListByUserId(userId);

        // 获取该用户角色对应的菜单权限
        List<String> checkedList = new ArrayList<String>();
        for (SysRoleMenus rm : roleMenuList) {
            String ridString = rm.getMenuId();
            checkedList.add(ridString);
        }

        //获取所有资源菜单树
        List<SysMenus> allMenuList = getAllPagedMenuList();


        for (SysMenus systemMenu : allMenuList) {
            MenuTreeBean a = new MenuTreeBean(systemMenu);
            List<MenuTreeBean> childList = getChild(systemMenu.getId(), allMenuList, checkedList);
            if ((checkedList.contains(systemMenu.getId())) && (systemMenu.getPid() == null || "".equals(systemMenu.getPid()))) {
                a.setChildren(childList);
                rootMenu.add(a);
            } else if (childList.size() > 0 && (systemMenu.getPid() == null || "".equals(systemMenu.getPid()))) {
                a.setChildren(childList);
                rootMenu.add(a);
            }
        }

        return rootMenu;
    }


    /**
     * 获取子节点
     *
     * @param id      父节点id
     * @param allMenu 所有菜单列表
     * @return 每个根节点下，所有子菜单列表
     */
    private List<MenuTreeBean> getChild(String id, List<SysMenus> allMenu, List<String> checkedlist) {
        // 子菜单
        List<MenuTreeBean> childList = new ArrayList<MenuTreeBean>();

        // 遍历所有节点，将所有菜单的父id与传过来的根节点的id比较
        for (SysMenus systemMenu : allMenu) {
            if (checkedlist.contains(systemMenu.getId()) || checkedlist.size() < 1) {
                // 相等说明：为该根节点的子节点。
                if (systemMenu.getPid() != null && (systemMenu.getPid().equals(id))) {
                    MenuTreeBean a = new MenuTreeBean(systemMenu);
                    a.setPid(systemMenu.getPid());
                    childList.add(a);
                }
            }
        }
        // 递归
        for (MenuTreeBean nav : childList) {
            nav.setChildren(getChild(nav.getId(), allMenu, checkedlist));
        }
        // 如果节点下没有子节点，返回一个空List（递归退出）
        if (childList.size() == 0) {
            return new ArrayList<MenuTreeBean>();
        }
        return childList;
    }


}
